package com.skivingcloud.report.ureport.service.impl;

import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.report.ureport.entity.UreportFileTbl;
import com.skivingcloud.report.ureport.mapper.UreportFileTblMapper;
import com.skivingcloud.report.ureport.service.IUreportFileTblService;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bstek.ureport.cache.CacheUtils;
import com.bstek.ureport.definition.ReportDefinition;
import com.bstek.ureport.export.ReportRender;
import com.bstek.ureport.parser.ReportParser;
import com.bstek.ureport.provider.report.ReportProvider;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-03-03
 */
@Service
public class UreportFileTblServiceImpl extends ServiceImpl<UreportFileTblMapper, UreportFileTbl> implements IUreportFileTblService {

	@Autowired
	private List<ReportProvider> reportProviders;
	@Autowired
	private ReportRender reportRender;
	@Autowired
	private ReportParser reportParser;
	
	@Override
	public void addOrUpdateUreportFileTbl(String fileName, String content) throws IOException {
		fileName = "db" + fileName;
		// 获取报表提供者-mysql
		ReportProvider targetReportProvider = null;
		for (ReportProvider provider : reportProviders) {
			if (fileName.startsWith(provider.getPrefix())) {
				targetReportProvider = provider;
				break;
			}
		}
		// 保存报表到数据库
		targetReportProvider.saveReport(fileName, content);
		// 处理缓存
		InputStream inputStream = null;
		try {
			inputStream = IOUtils.toInputStream(content, "utf-8");
			ReportDefinition reportDef = reportParser.parse(inputStream, fileName);
			reportRender.rebuildReportDefinition(reportDef);
			CacheUtils.cacheReportDefinition(fileName, reportDef);
		} catch (IOException e) {
			throw e;
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<UreportFileTbl> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		UreportFileTbl ureportFileTbl = (UreportFileTbl) JSONObject
				.toBean(JSONObject.fromObject(params.get("dataForm")), UreportFileTbl.class);
		QueryWrapper<UreportFileTbl> queryWrapper = new QueryWrapper<UreportFileTbl>();
		if (ureportFileTbl != null) {
			if (StringUtils.isNotBlank(ureportFileTbl.getName())) {
				queryWrapper.like("name", "%" + ureportFileTbl.getName().trim() + "%");
			}
		}
		List<UreportFileTbl> list = this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	@Override
	public void delete(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

}
